//
//  NSString+Zego.m
//  TTProduct
//
//  Created by Taurin on 2019/12/30.
//  Copyright © 2019 TTProduct. All rights reserved.
//

#import "NSString+Zego.h"

@implementation NSString (Zego)

- (NSData *)tt_toSignString {
    if(self.length == 0) return nil;
    NSString *strSign = [self lowercaseString];
    strSign = [strSign stringByReplacingOccurrencesOfString:@" " withString:@""];
    strSign = [strSign stringByReplacingOccurrencesOfString:@"0x" withString:@""];
    NSArray* szStr = [strSign componentsSeparatedByString:@","];
    int nLen = (int)[szStr count];
    Byte szSign[32];
    for(int i = 0; i < nLen; i++) {
        NSString *strTmp = [szStr objectAtIndex:i];
        if(strTmp.length == 1) {
            szSign[i] = [strTmp tt_toByte];
        }
        else {
            szSign[i] = [[strTmp substringWithRange:NSMakeRange(0, 1)] tt_toByte] << 4 |
                        [[strTmp substringWithRange:NSMakeRange(1, 1)] tt_toByte];
        }
    }
    
    NSData *sign = [NSData dataWithBytes:szSign length:32];
    return sign;
}

+ (NSData *)tt_stringWithSignString:(NSString *)strSign {
    return strSign.tt_toSignString;
}

- (Byte)tt_toByte {
    NSString *str = @"0123456789abcdef";
    Byte b = [str rangeOfString:self].location;
    return b;
}

@end
